package team.seekers.hula.system.phoneMessage.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import team.seekers.hula.common.entity.result.ApiExceptionEnum;
import team.seekers.hula.common.utils.AssertUtils;
import team.seekers.hula.common.utils.RedisConstants;
import team.seekers.hula.common.utils.RedisUtils;
import team.seekers.hula.system.phoneMessage.entity.TxSmsTemplate;

import java.util.ArrayList;
import java.util.Optional;

@RestController
public class PhoneMessageService {

    @Autowired
    private TxSmsTemplate txSmsTemplate;

    @Autowired
    private RedisUtils redisUtils;

    public Void sendMessage(String phone, ArrayList<String> params) {
        System.out.println(params);
        String Msg = txSmsTemplate.sendMesModel(phone, params);
        // Msg不为null 发送成功
        // Msg为null  发送失败
        System.out.println(Msg);
        return null;
    }

    /**
     * @Description 进行验证码验证
     **/
    public void verifyCaptcha(String captchaId, String inputCaptcha) {
        AssertUtils.notNull(captchaId, ApiExceptionEnum.CAPTCHA_NOT_FOUND);
        AssertUtils.notNull(inputCaptcha, ApiExceptionEnum.CAPTCHA_NOT_FOUND);

        String captcha = Optional.ofNullable(redisUtils.get(RedisConstants.getCaptchaKey(captchaId))).map(o -> (String) o).orElse(null);

        AssertUtils.notNull(captcha, ApiExceptionEnum.CAPTCHA_NOT_MATCHING);
        AssertUtils.isTrue(captcha.equalsIgnoreCase(inputCaptcha), ApiExceptionEnum.CAPTCHA_NOT_MATCHING);
    }
}
